package com.ruoyi.wetest.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wetest.mapper.WetestStatLogMapper;
import com.ruoyi.wetest.domain.WetestStatLog;
import com.ruoyi.wetest.service.IWetestStatLogService;

/**
 * 压测数据统计时间记录Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-16
 */
@Service
public class WetestStatLogServiceImpl implements IWetestStatLogService {
    @Autowired
    private WetestStatLogMapper wetestStatLogMapper;

    /**
     * 查询压测数据统计时间记录
     *
     * @param id 压测数据统计时间记录主键
     * @return 压测数据统计时间记录
     */
    @Override
    public WetestStatLog selectWetestStatLogById(Long id) {
        return wetestStatLogMapper.selectWetestStatLogById(id);
    }

    /**
     * 查询压测数据统计时间记录列表
     *
     * @param wetestStatLog 压测数据统计时间记录
     * @return 压测数据统计时间记录
     */
    @Override
    public List<WetestStatLog> selectWetestStatLogList(WetestStatLog wetestStatLog) {
        return wetestStatLogMapper.selectWetestStatLogList(wetestStatLog);
    }

    /**
     * 新增压测数据统计时间记录
     *
     * @param wetestStatLog 压测数据统计时间记录
     * @return 结果
     */
    @Override
    public int insertWetestStatLog(WetestStatLog wetestStatLog) {
        return wetestStatLogMapper.insertWetestStatLog(wetestStatLog);
    }

    /**
     * 修改压测数据统计时间记录
     *
     * @param wetestStatLog 压测数据统计时间记录
     * @return 结果
     */
    @Override
    public int updateWetestStatLog(WetestStatLog wetestStatLog) {
        return wetestStatLogMapper.updateWetestStatLog(wetestStatLog);
    }

    /**
     * 批量删除压测数据统计时间记录
     *
     * @param ids 需要删除的压测数据统计时间记录主键
     * @return 结果
     */
    @Override
    public int deleteWetestStatLogByIds(Long[] ids) {
        return wetestStatLogMapper.deleteWetestStatLogByIds(ids);
    }

    /**
     * 删除压测数据统计时间记录信息
     *
     * @param id 压测数据统计时间记录主键
     * @return 结果
     */
    @Override
    public int deleteWetestStatLogById(Long id) {
        return wetestStatLogMapper.deleteWetestStatLogById(id);
    }

    @Override
    public int saveBatchWetestStatLog(List<WetestStatLog> trans) {
        int pace = 500;
        if (trans.size() > 1000) {
            List<List<WetestStatLog>> divideList = divideList(trans, pace);
            for (List<WetestStatLog> wetestStatLogs : divideList) {
                wetestStatLogMapper.saveBatchWetestStatLog(wetestStatLogs);
            }
        }else {
            wetestStatLogMapper.saveBatchWetestStatLog(trans);
        }
        return 0;
    }

    private List<List<WetestStatLog>> divideList(List<WetestStatLog> list, int chunkSize) {
        List<List<WetestStatLog>> dividedList = new ArrayList<>();
        int fromIndex = 0;
        int toIndex;
        while (fromIndex < list.size()) {
            toIndex = Math.min(fromIndex + chunkSize, list.size());
            dividedList.add(new ArrayList<>(list.subList(fromIndex, toIndex)));
            fromIndex = toIndex;
        }
        return dividedList;
    }
}
